   
var cookieOpt = {
  expires: 100,
  path: '/',
  domain: 'xueerqin.net'
}

  function getQueryVariable(variable) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
      var pair = vars[i].split("=");
      if (pair[0] == variable) {
        return pair[1];
      }
    }
    return (false);
  }


  function isFirst(){
      var first = $.cookie('first')
      debugger
      if(first){
      
          $.cookie('first', '', {
            // expires: 100,
            path: '/',
            domain: 'xueerqin.net'
        })

          wx.miniProgram.navigateBack()
          return false
      } else {
        
          $.cookie('first','1', {
            // expires: 100,
            path: '/',
            domain: 'xueerqin.net'
          })
          return true
      }
  }

  function toUrl (url) {
         
    if (url.indexOf('ClassScheduleV2.shtml') != -1) {
   
      location.replace(busi + '/study/index.html#/timetable/timetable')
      return
    }


    var index =  url.indexOf('#')
    if (index != -1) {
      var hash = url.split('#')[1]
      if (/^\//.test(hash)) {
       
      } else {
     
        hash = '/' + hash
        
      }
      $.cookie('miniPath', hash, {
        path: '/',
        domain: '.xueerqin.net',
        expires: 100
      })
      location.href = busi + '/study/index.html#' + hash
    }else {
   
      location.href = url
    }
  }


  function loginSuccess(res){
    $.cookie('authToken', JSON.stringify(res.oAuth), {
      path: '/',
      domain: '.xueerqin.net',
      expires: 100
    })

    $.cookie("loginName", res.loginName, { expires: 100, path: '/', domain: 'xueerqin.net' });
    $.cookie("cellPhoneNum", res.loginName, { expires: 100, path: '/', domain: 'xueerqin.net' });

    // $.cookie('isMoreRole', false, {expires: 100, path: '/', domain: 'xueerqin.net'})
    var roleCode = $.cookie('roleCode')
    var classId = $.cookie('classId')
    var studentId = $.cookie('studentId')
    if(roleCode == 'Teacher'){
      localStorage.setItem('itemDataTeacher', JSON.stringify({
        ClassId: classId
      }))
    } else if(roleCode == 'Parent'){
      localStorage.setItem('itemData', JSON.stringify({
        SelfId: studentId
      }))
    }
    
    toUrl($.cookie('webUrl'))
  }




  function start(){


      var roleCode = getQueryVariable('roleCode')
      var access_token = getQueryVariable('access_token')
      var webUrl = getQueryVariable('webUrl')
      var classId = getQueryVariable('classId')
      var studentId = getQueryVariable('studentId')
      var phone = getQueryVariable('phone')
      var code = getQueryVariable('code')
      var latitude = getQueryVariable('latitude')
      var longitude = getQueryVariable('longitude')
    
      $.cookie('isMini', 1, {path: '/'})

      if(roleCode){
        $.cookie('roleCode',roleCode, {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
      if(webUrl){
        $.cookie('webUrl',decodeURIComponent(webUrl), {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
      if(classId){
        $.cookie('classId',classId, {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
  
      if(studentId){
        $.cookie('studentId', studentId, {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
      if(phone){
        $.cookie("loginName", phone, { expires: 100, path: '/', domain: 'xueerqin.net' });
        $.cookie("cellPhoneNum", phone, { expires: 100, path: '/', domain: 'xueerqin.net' });
      }
      if(code){
        $.cookie('code', code, {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
       
      if(latitude){
        $.cookie('latitude', latitude, {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
      if(longitude){
        $.cookie('longitude', longitude, {expires: 100, path: '/', domain: 'xueerqin.net'})
      }
     
      if(access_token){
        $.cookie('authToken', JSON.stringify({
            access_token: access_token,
            expires_in: 3600,
            token_type: 'Bearer'
        }), {
            expires: 100,
            path: '/',
            domain: 'xueerqin.net'
        })
      }
      
   

     
      

      
      
      if(code){
        userWechatInfoForComponent().then(res => {
            
            openid = res.openId
            unionid = res.unionId
        
            
            return miniProgramLogin({
              openid: openid,
              unionid: unionid,
              appid: appid
            })
        
        }).then((res) => {
          debugger
            loginSuccess(res)
            
        })
      } else {
        miniProgramLogin({
          appid: appid
        }).then(res => {
          debugger
          if(!res.openId){
            wechatLogin()
            return
          }

          $.cookie('OpenId', res.openId, { expires: 100, path: '/', domain: 'xueerqin.net' });
          $.cookie('unionID', res.unionId, { expires: 100, path: '/', domain: 'xueerqin.net' });
          
          return miniProgramLogin({
            appid: appid,
            openid: res.openid,
            unionid: res.unionid
          })
          
        }).then((res) => {
          debugger
            loginSuccess(res)
        })
    }


      
    
      

           
      

           
        
  
  }





(function(){
  try {
    start()
  } catch (error) {
    document.writeln(JSON.stringify(error))
  }
   
})()
  
  